const Router = require('koa-router');
const router = new Router();
const gravatar = require('gravatar');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const passport = require('koa-passport')

// 引入 tools
const tools = require("../../config/tools")
// 引入 User
const User = require("../../models/User")
// 引入key
const keys = require("../../config/keys")
// 引入验证
const validationRegIpt = require('../../validation/register')
const validationLogIpt = require('../../validation/login')

/**
 * @route GET api/users/test
 * @desc 测试接口
 * @access 接口是公开的
 * 
 */
router.get("/test", async ctx => {
  ctx.status = 200;
  ctx.body = {
    msg: 'user works...'
  };
})

/**
 * @route POST api/users/register
 * @desc 注册接口地址
 * @access 接口是公开的
 * 
 */
router.post("/register", async ctx => {
  // console.log(ctx.request.body);

  const { errors, isValid } = validationRegIpt(ctx.request.body);
  // 判断是否验证码通过
  if(!isValid){
    ctx.status = 400;
    ctx.body = errors;
    return;
  }

  // 存储到数据库
  const findResult = await User.find({
    email: ctx.request.body.email
  })
  console.log(findResult);
  if (findResult.length > 0) {
    ctx.status = 500;
    ctx.body = {
      "email": "邮箱已被占用"
    };
  } else {
    // 没查到
    const avatar = gravatar.url(ctx.request.body.email, { s: '200', r: 'pg', d: 'mm' }); //pg格式，mm默认头像
    const newUser = new User({
      name: ctx.request.body.name,
      email: ctx.request.body.email,
      avatar:avatar,
      password: tools.enbcrypt(ctx.request.body.password),
    });
    // console.log(newUser);
    // 存储到数据库
    await newUser.save().then((user) => {
      ctx.body = user;
    }).catch((error) => {
      console.log(error);
    })
    // 返回json数据
    ctx.body = newUser;
  }
})

/**
 * @route POST api/users/login
 * @desc 登录接口地址 返回token
 * @access 接口是公开的
 * 
 */
 router.post("/login", async ctx => {
  const { errors, isValid } = validationLogIpt(ctx.request.body);
  // 判断是否验证码通过
  if(!isValid){
    ctx.status = 400;
    ctx.body = errors;
    return;
  }
  // 查询
  const findResult =  await User.find({ email:ctx.request.body.email });
  const user = findResult[0]
  const password = ctx.request.body.password;
  // 判断是否查到
  if(findResult.length == 0){
    ctx.status = 404;
    ctx.body = {'email':"用户不存在"}
  }else{
    // 查到后 验证密码
    var result = await bcrypt.compareSync(password, user.password);
    // 验证通过
    if(result) {
      // 返回token
      const payload = { id:user.id, name:user.name, avatar:user.avatar }
      // 签名sign
      const token = jwt.sign(payload, keys.secretKey, { expiresIn:3600 })
      ctx.status = 200;
      ctx.body = { success:true, token:"Bearer "+ token }; //注意这里的格式  Bearer 后面有个空格
    } else {
      ctx.status = 400,
      ctx.body = { password:"密码错误" };
    }
  }
})


/**
 * @route GET api/users/current
 * @desc 用户信息地址 返回用户信息
 * @access 接口是私密的
 * 
 */
 router.get("/current", passport.authenticate('jwt', { session: false }), async ctx => {
  // ctx.body = { success:true }
  ctx.body = {
    id:ctx.state.user.id,
    name:ctx.state.user.name,
    email:ctx.state.user.email,
    avatar:ctx.state.user.avatar,
  }
})

module.exports = router.routes();